import 'package:boss/models/commodity.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/ui/page/commodity/categories/modal/deleteCommodityAlert/delete_commodity_alert.dart';
import 'package:boss/ui/page/commodity/categories/modal/stockModal/stock_modal.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/plus_commodity_by_form_field_page.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/plus_package_page.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

import 'categories_commodity_item.dart';

class CategoriesCommodityList extends StatelessWidget {
  final ItemScrollController scrollController;
  final List<Commodity> commodities;

  const CategoriesCommodityList({
    @required this.commodities,
    this.scrollController,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (commodities.isEmpty) {
      return EmptyWidget(title: '暂无数据');
    }

    final provider = Provider.of<CommodityProvider>(context, listen: false);

    return ScrollablePositionedList.builder(
      itemScrollController: scrollController,
      itemCount: commodities.length,
      padding: EdgeInsets.only(bottom: 50),
      itemBuilder: (context, index) {
        final current = commodities[index];

        return CategoriesCommodityItem(
          data: current,
          index: index,
          isShowSku: false,
          batchType: provider.batchType,
          onTap: () {
            if (provider.batchType == BatchType.changeClassification || provider.batchType == BatchType.delete) {
              provider.toggleCommoditySelected(current);
            } else if (provider.batchType == BatchType.normal) {
              if (current.commodityType == Commodity.COMMODITY_TYPE_PACKAGE) {
                Navigator.pushNamed(context, PlusPackagePage.pageName, arguments: {
                  'package': current,
                });
              } else {
                Navigator.pushNamed(context, PlusCommodityByFormFieldPage.pageName, arguments: {
                  'commodity': Commodity.fromJson(current.toJson()),
                });
              }
            }
          },
          onNumberChanged: (value) {
            provider.setCommodityCount(current, value);
          },
          onDeleted: () {
            deleteCommodityAlert(
              context,
              onConfirm: (context) async {
                Navigator.pop(context);
                provider.deleteCommodityByBean(current);
              },
            );
          },
          onToggleSale: () {
            provider.toggleSale(current);
          },
          onStock: () {
            showStockModal(context, current);
          },
        );
      },
    );
  }
}
